package com.cskaoyan.service.keyword;

import com.cskaoyan.bean.MallKeyword;
import com.cskaoyan.bean.MallKeywordExample;
import com.cskaoyan.mapper.MallKeywordMapper;
import com.cskaoyan.model.bo.keyword.PageBO2;
import com.cskaoyan.model.bo.keyword.create.KeywordCreateBO;
import com.cskaoyan.model.bo.keyword.delete.KeywordDeleteBO;
import com.cskaoyan.model.vo.keyword.KeywordVO;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @program: project-market
 * @description: 关键词
 * @author: shiyue
 * @create: 2021/11/30 20:30
 **/
@Service
public class KeywordServiceImpl implements KeywordService {
    @Autowired
    MallKeywordMapper mapper;

    @Override
    public KeywordVO getKeyword(PageBO2 pageBo) {

        PageHelper.startPage(pageBo.getPage(), pageBo.getLimit());
        MallKeywordExample mallKeywordExample = new MallKeywordExample();
        mallKeywordExample.setOrderByClause(pageBo.getSort() + " " + pageBo.getOrder());

        String keyword = pageBo.getKeyword();
        String url = pageBo.getUrl();

        if (keyword == null) keyword = "";
        if (url == null) url = "";

        mallKeywordExample.createCriteria().andKeywordLike("%" + keyword + "%").andUrlLike("%" + url + "%").andDeletedEqualTo(false);

        List<MallKeyword> keywords = mapper.selectByExample(mallKeywordExample);
        PageInfo pageInfo = new PageInfo(keywords);
        Integer total = Math.toIntExact(pageInfo.getTotal());
        return new KeywordVO(total, pageBo.getLimit(), pageBo.getPage(), keywords);
    }

    @Override
    public int createKeyword(KeywordCreateBO keywordCreateBO) {
        MallKeyword record = new MallKeyword();
        record.setKeyword(keywordCreateBO.getKeyword());
        record.setUrl(keywordCreateBO.getUrl());
        record.setIsHot(keywordCreateBO.getIsHot());
        record.setIsDefault(keywordCreateBO.getIsDefault());
        Date date = new Date();
        record.setAddTime(date);
        record.setUpdateTime(date);
        record.setDeleted(false);
        record.setSortOrder(0);

        int row = mapper.insert(record);
        int id = record.getId();

        System.out.println(id);
        return id;
    }

    @Override
    public MallKeyword getOneKeyword(int id) {
        MallKeyword mallKeyword = mapper.getOneKeyword(id);
        return mallKeyword;
    }

    @Override
    public int deleteKeyword(Integer id) {
        int row = mapper.deleteByPrimaryKey(id);
        return row;
    }

    @Override
    public int updateKeyword(KeywordDeleteBO bo) {
        MallKeyword record = new MallKeyword();
        record.setId(bo.getId());
        record.setKeyword(bo.getKeyword());
        record.setUrl(bo.getUrl());
        record.setIsHot(bo.getIsHot());
        record.setIsDefault(bo.getIsDefault());

        Date date = new Date();
        record.setAddTime(bo.getAddTime());
        record.setUpdateTime(date);
        record.setDeleted(bo.getDeleted());
        record.setSortOrder(bo.getSortOrder());

        int row = mapper.updateByPrimaryKeySelective(record);
        return row;
    }
}
